package cn.gson.springboot.model.service.privilegeservice;

import cn.gson.springboot.model.dao.privilegedao.EmpDao;
import cn.gson.springboot.model.mapper.privilegemapper.UserLoginMapper;
import cn.gson.springboot.model.pjos.Emp;
import cn.gson.springboot.model.pjos.Menu;
import cn.gson.springboot.model.pjos.Permissionsset;
import cn.gson.springboot.model.vo.privilegevo.EmpVo;
import cn.gson.springboot.model.vo.privilegevo.Router;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserLoginService {
    @Autowired
    UserLoginMapper userLoginMapper;
    @Autowired
    EmpDao empDao;


    //验证登录
    public Emp login(Emp emp){
        return userLoginMapper.emp(emp);
    }

    //左边菜单栏
    public List<Permissionsset> user_meun(Integer employeesId){
        return userLoginMapper.user_meun(employeesId);
    }

    //按钮权限
    public List<Menu> perssionButton(Integer employeesId){
        return userLoginMapper.perssionButton(employeesId);
    }

    //路由
    public List<Router> routerList(Integer employeesId){
        List<Router> router1 = ownRouter();
        List<Router> routerList = userLoginMapper.routerList(employeesId);
        for (int i=0;i<router1.size();i++){
            routerList.get(0).getChildren().add(router1.get(i));
        }
        return  routerList;
    }
    //每个人拥有的初始化路由
    public List<Router> ownRouter(){
        return userLoginMapper.ownRouter();
    }
    /**
     * 用于个人中心
     * @param employeesId
     * @return
     */
    public Emp personal(Integer employeesId){
        return userLoginMapper.personal(employeesId);
    }

    /**
     * 旧密码是否正确
     */
    public int yzPwd(Integer employeesId,String oldPwd){
        Emp emp = empDao.empInfo(employeesId);//查询用户的旧密码
        if(emp.getEmpPwd().equals(oldPwd)){
            return 1;//旧密码正确
        }else{
            return 0;//旧密码错误
        }
    }


    /**;
     *  修改密码
     */
    public void updatePwd(Integer employeesId,String newPwd){
        Emp emp  = empDao.findById(employeesId).get();
        emp.setEmpPwd(newPwd);
        empDao.save(emp);
    }

    /**
     * 重置密码
     */
    public void restPwd(Integer employeesId){
        Emp emp  = empDao.findById(employeesId).get();
        emp.setEmpPwd("r1ThEqAQooVzZFGoG2gIjg==");
        empDao.save(emp);
    }

    /**
     * 修改用户部门信息
     * @param emp
     */
    public void updateSomeInfo(EmpVo emp) {
        Emp emp1 = empDao.findById(emp.getEmployeesId()).get();
        emp1.setEmpPhone(emp.getEmpPhone());
        emp1.setEmpAddressProvince(emp.getEmpAddressProvince());
        emp1.setEmpAddressCity(emp.getEmpAddressCity());
        emp1.setEmpAddressArea(emp.getEmpAddressArea());
        emp1.setEmpAddressDetailed(emp.getEmpAddressDetailed());
        empDao.save(emp1);
    }
    /**
     * 上传头像
     */
    public void uploadPath(Integer employeesId,String path){
        Emp emp = empDao.findById(employeesId).get();
        System.out.println("头像路径："+path);
        emp.setEmpImgpath(path);
        empDao.save(emp);
    }
}
